package com.sky.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetmealDishesMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author: Yosoo
 * @Date: 2025/5/19 08:34
 * @Description:
 */
@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishesMapper setmealDishesMapper;

    /**
     * 新增套餐
     * @Author:     Yosoo
     * @Date:       2025/5/19 08:40
     * @Description:
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void save(SetmealDTO setmealDTO) {
        // 1.保存套餐数据
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.insert(setmeal);

        // 2.保存套餐和菜品的关联关系
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmeal.getId());
        });
        setmealDishesMapper.insertBatch(setmealDishes);
    }

    @Override
    public SetmealVO getById(Long id) {
        return setmealMapper.getById(id);
    }

    @Override
    public void update(Setmeal setmeal) {
        this.updates(setmeal);
    }

    @Override
    public void updateStatus(Setmeal setmeal) {
        this.updates(setmeal);
    }

    /**
     * 批量删除套餐
     * @Author:     Yosoo
     * @Date:       2025/5/19 09:55
     * @Description:
     * @param ids
     */
    @Transactional
    @Override
    public void deleteBatch(List<Long> ids) {
        for (Long id : ids) {
            SetmealVO setmealVO = setmealMapper.getById(id);
            if(ObjectUtil.equal(setmealVO.getStatus(), StatusConstant.ENABLE)){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        // 1.删除套餐中的数据
        setmealMapper.deleteByIds(ids);

        // 2.删除套餐和菜品的关联关系
        setmealDishesMapper.deleteBySetmealIds(ids);
    }

    /**
     * 修改套餐
     * @Author:     Yosoo
     * @Date:       2025/5/19 09:53
     * @Description:
     * @param setmeal
     */
    public void updates(Setmeal setmeal){
        setmealMapper.update(setmeal);
    }

    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        Setmeal setmeal = Setmeal.builder()
                .name(setmealPageQueryDTO.getName())
                .categoryId(setmealPageQueryDTO.getCategoryId())
                .status(setmealPageQueryDTO.getStatus())
                .build();
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<Setmeal> page = setmealMapper.pageQuery(setmeal);
        return new PageResult(page.getTotal(),page.getResult());
    }
}
